package com.caigou.modules.data.ui.data

import android.os.Bundle
import android.text.TextUtils
import android.widget.CheckBox
import androidx.activity.viewModels
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.constants.AppConstants
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseActivity
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.ext.observe
import com.caigou.comm.base.ext.showToast
import com.caigou.comm.base.widget.SpaceItemDecoration
import com.caigou.modules.data.R
import com.caigou.modules.data.adpter.DataItemAdapter
import com.caigou.modules.data.adpter.DataTypeItemAdapter
import com.caigou.modules.data.bean.DataItemBean
import com.caigou.modules.data.bean.DataTypeItemBean
import com.caigou.modules.data.databinding.ActivityDataBinding
import com.caigou.modules.data.viewmodel.DataViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/12/29
 */
@Route(path = RouterPath.Data.PAGE_DATA)
@AndroidEntryPoint
class DataActivity : BaseActivity() {
    private lateinit var binding: ActivityDataBinding
    private val dataViewModel: DataViewModel by viewModels()

    // 默认第一个
    private var checkPos = 0

    private val dataTypeAdapter: DataTypeItemAdapter by lazy {
        DataTypeItemAdapter(mutableListOf())
    }

    private val dataAdapter: DataItemAdapter by lazy {
        DataItemAdapter(mutableListOf())
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityDataBinding.inflate(layoutInflater)
        setContentView(binding.root)
        containerId = binding.dataLlContainer.id
        initView()
    }

    private fun initView() {
        ARouter.getInstance().inject(this)

        binding.dataSwipeRefreshLayout.run {
            setColorSchemeColors(resources.getColor(R.color.caigou_theme_color))
            setOnRefreshListener {
                fetchDataList()
            }
        }

        binding.dataRecyclerViewType.run {
            layoutManager = GridLayoutManager(this@DataActivity, 4)
            adapter = dataTypeAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(SpaceItemDecoration(this@DataActivity))
        }

        dataTypeAdapter.setOnItemClickListener { _, view, position ->
            if (checkPos != position) {
                dataTypeAdapter.checkBoxes[checkPos].isChecked = false
                checkPos = position
            }
            (view as CheckBox).isChecked = true
            fetchDataList()
        }

        binding.dataRecyclerViewDataList.run {
            layoutManager = LinearLayoutManager(this@DataActivity)
            adapter = dataAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(SpaceItemDecoration(this@DataActivity))
        }

        dataAdapter.setOnItemClickListener { _, view, position ->
            val bean = dataAdapter.getItem(position)
            ARouter.getInstance().build(RouterPath.Data.PAGE_DATA_DETAIL)
                .withInt(AppConstants.CATEGORY_ID, bean.categoryId)
                .withString(AppConstants.CATEGORY_NAME, dataTypeAdapter.getItem(checkPos).categoryName)
                .withInt(AppConstants.DATA_ID, bean.mysteryId)
                .navigation()

        }

        binding.dataTvLaw.setOnClickListener {
            ARouter.getInstance().build(RouterPath.Data.PAGE_LAW).navigation()
        }

        binding.dataTvAllYear.setOnClickListener {
            ARouter.getInstance().build(RouterPath.Data.PAGE_ALL_YEAR).navigation()
        }

        binding.dataTvBack.setOnClickListener {
            finish()
        }

        fetchDataTypeList()
    }

    override fun observeViewModel() {
        super.observeViewModel()
        observe(dataViewModel.dataTypeListLiveData, ::handleGetDataTypeList)
        observe(dataViewModel.dataListLiveData, ::handleGetDataList)
    }

    private fun handleGetDataTypeList(result: Resource<MutableList<DataTypeItemBean>>) {
        when (result) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                binding.dataSwipeRefreshLayout.isRefreshing = false
                if (!TextUtils.isEmpty(result.errorMsg)) {
                    showToast(result.errorMsg)
                } else {
                    showToast("获取信息异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                binding.dataSwipeRefreshLayout.isRefreshing = false
                result.data?.apply {
                    dataTypeAdapter.setList(this)
                    fetchDataList()
                }
            }
        }
    }

    private fun handleGetDataList(result: Resource<MutableList<DataItemBean>>) {
        when (result) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                binding.dataSwipeRefreshLayout.isRefreshing = false
                if (!TextUtils.isEmpty(result.errorMsg)) {
                    showToast(result.errorMsg)
                } else {
                    showToast("获取信息异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                binding.dataSwipeRefreshLayout.isRefreshing = false
                result.data?.apply {
                    binding.dataTvTypeTitle.text = "精选资料>${dataTypeAdapter.getItem(checkPos).categoryName}"
                    dataAdapter.setList(this)
                }
            }
        }
    }

    private fun fetchDataTypeList() {
        dataViewModel.getDataTypeList()
    }

    private fun fetchDataList() {
        val categoryId = dataTypeAdapter.getItem(checkPos).categoryId
        dataViewModel.getDataList(categoryId)
    }

}